package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"

	"hc-admin/app/admin/models"
	"hc-admin/app/admin/service/dto"
	"hc-admin/common/actions"
	cDto "hc-admin/common/dto"
)

type BusUserData struct {
	service.Service
}

// GetPage 获取BusUserData列表
func (e *BusUserData) GetPage(c *dto.BusUserDataGetPageReq, p *actions.DataPermission, list *[]models.BusUserData, count *int64) error {
	var err error
	var data models.BusUserData

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("BusUserDataService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取BusUserData对象
func (e *BusUserData) Get(d *dto.BusUserDataGetReq, p *actions.DataPermission, model *models.BusUserData) error {
	var data models.BusUserData

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetBusUserData error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建BusUserData对象
func (e *BusUserData) Insert(c *dto.BusUserDataInsertReq) error {
	var err error
	var data models.BusUserData
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("BusUserDataService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改BusUserData对象
func (e *BusUserData) Update(c *dto.BusUserDataUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.BusUserData{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("BusUserDataService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

func (e *BusUserData) UpdateBalnce(c *dto.BusUserDataBalanceUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.BusUserData{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("UpdateBalnce Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// Remove 删除BusUserData
func (e *BusUserData) Remove(d *dto.BusUserDataDeleteReq, p *actions.DataPermission) error {
	var data models.BusUserData

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveBusUserData error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}
